package br.fic.poo.agenda.mainapplication;

import java.awt.BorderLayout;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Window.Type;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class MensagemAutomatica extends Thread
{

   public static final Integer ACAO_SUCESSO = 1;

   public static final Integer ACAO_ERRO = 2;

   private JDialog frame;

   private JButton botaoOk;

   private Boolean sair = false;

   public MensagemAutomatica(String titulo, String mensagem, Integer acao)
   {
      frame = new JDialog();
      frame.setModal(true);
      frame.setType(Type.POPUP);
      frame.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);

      JLabel labelMensagem = new JLabel(mensagem);

      Integer width = (int) labelMensagem.getPreferredSize().getWidth() + 150;
      Integer height = (int) labelMensagem.getPreferredSize().getHeight();
      
      if (height < 40)
      {
         height = 130;
      }
      else
      {
         height += 100;
      }

      frame.setSize(width, height);
      frame.setLocationRelativeTo(null);
      frame.setTitle(titulo);
      frame.getContentPane().setLayout(new BorderLayout(0, 0));
      Image icon = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE);
      frame.setIconImage(icon);

      JPanel painelBotaoFechar = new JPanel();
      frame.getContentPane().add(painelBotaoFechar, BorderLayout.SOUTH);

      botaoOk = new JButton("OK");
      painelBotaoFechar.add(botaoOk);
      botaoOk.setFont(new Font("Tahoma", Font.PLAIN, 11));
      botaoOk.setPreferredSize(new Dimension(73, 23));
      botaoOk.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            frame.dispose();
         }
      });

      JPanel painelConteudo = new JPanel();
      frame.getContentPane().add(painelConteudo, BorderLayout.CENTER);
      painelConteudo.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("max(8dlu;default)"),
         FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("max(31dlu;default)"), FormFactory.RELATED_GAP_COLSPEC,
         FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
         FormFactory.RELATED_GAP_ROWSPEC, RowSpec.decode("max(5dlu;default)"), FormFactory.RELATED_GAP_ROWSPEC,
         FormFactory.DEFAULT_ROWSPEC, }));

      JLabel imagemMensagem = new JLabel("");

      if (acao == ACAO_SUCESSO)
      {
         imagemMensagem.setIcon(new ImageIcon(MensagemAutomatica.class.getResource("/br/fic/poo/images/informationMessage.png")));
      }
      else
      {
         imagemMensagem.setIcon(new ImageIcon(MensagemAutomatica.class.getResource("/br/fic/poo/images/errorMessage.png")));
      }

      painelConteudo.add(imagemMensagem, "4, 4");

      painelConteudo.add(labelMensagem, "6, 4");

      start();
      frame.setVisible(true);
   }

   @Override
   public void run()
   {
      try
      {
         while (!sair)
         {
            for (int i = 5; i >= 0; i--)
            {
               botaoOk.setText("OK (" + i + ")");
               Thread.sleep(1000);
            }
            sair = true;
            frame.dispose();
         }
      }
      catch (InterruptedException e)
      {
         e.printStackTrace();
      }
   }
}
